/*
 * CommentBean.java
 *
 * Created on 13 novembre 2007, 16:35
 */
package com.reimscopie.beans.entities ;

import java.util.* ;

import javax.persistence.* ;

import com.reimscopie.* ;


/**
 * This entity bean stores a comment made on a product.
 */
@Entity
public class CommentBean extends ValueBean<Comment> {
  
  // comment content
  private String message ;
  
  // date of commitment
  private Date date ;
  
  // the account corresponding to this comment
  private AccountBean account ;
  
  
  /**
   * Create a new instance of <code>CommentBean</code> (default constructor).
   */
  public CommentBean() {
  }
  
  
  /**
   * Create a new instance of <code>CommentBean</code> with all attributes.
   *
   * @param comment The valued comment.
   */
  public CommentBean(Comment comment) {
    
    if (comment.hasId())
      throw new IllegalArgumentException("comment has an image") ;
    
    this.message = comment.getMessage() ;
    this.date    = comment.getDate() ;
    
  }
  
  
  /**
   * Return the content of the comment.
   *
   * @return The comment message.
   */
  public String getMessage() {
    return message ;
  }
  
  
  /**
   * Sets the content message of this comment.
   *
   * @param message The new comment message.
   */
  public void setMessage(String message) {
    this.message = message ;
  }
  
  
  /**
   * Return the commitment date of the comment.
   *
   * @return The comment date.
   */
  public Date getDate() {
    return date ;
  }
  
  
  /**
   * Sets the commitment date of the message.
   *
   * @param date The new date.
   */
  public void setDate(Date date) {
    this.date = date ;
  }
  
  
  /**
   * Return the account corresponding to this comment.
   *
   * @return the account corresponding to this comment.
   */
  @ManyToOne(optional=false)
  public AccountBean getAccount() {
    return account ;
  }
  
  
  /**
   * Set the account corresponding to this comment
   *
   * @param account The account corresponding to this comment
   */
  public void setAccount(AccountBean account) {
    this.account = account ;
  }
  
  
////////////////////////////////////////////////////////////////////////////////
//                           TRANSFOMATION                                    //
////////////////////////////////////////////////////////////////////////////////

  /**
   * Transforms this entity bean into the associated comment.
   *
   * @return A new comment instance.
   *
   * @see #toValue
   */
  public Comment toComment() {
    return toValue() ;
  }

  
  /**
   * Transform this entity bean into the associated comment.
   * 
   * @return A new comment instance.
   */
  @Override
  public Comment toValue() {
    
    return new Comment(
        getId(),
        getMessage(),
        getDate(),
        getAccount().toAccount()
    ) ;
    
  }
  
}
